<?php
//  +----------------------------------------------------------------------
//  | huicmf [ huicmf快速开发框架 ]
//  +----------------------------------------------------------------------
//  | Copyright (c) 2022~2024 https://xiaohuihui.cc All rights reserved.
//  +----------------------------------------------------------------------
//  | Author: 小灰灰 <762229008@qq.com>
//  +----------------------------------------------------------------------
//  | Info:
//  +----------------------------------------------------------------------
//
namespace plugin\admin\app\controller;

use support\Request;
use think\facade\Db;
use plugin\admin\app\common\Auth;

class IndexController extends CrudController
{

    /**
     * 无需登录的方法
     * @var string[]
     */
    protected $noNeedLogin = ['index', 'login'];

    /**
     * 不需要鉴权的方法
     * @var string[]
     */
    protected $noNeedAuth = ['dashboard'];

    public function index()
    {
        if ( ! is_file(base_path().'/.env')) {
            return view('install/index');
        }

        clearstatcache();
        $admin = admin();
        if ( ! $admin) {
            return redirect('/app/admin/index/login');
        }

        $version = config('plugin.admin.app.version', '');

        return view('index/index', ['version' => $version]);
    }

    public function login()
    {
        $admin = admin();
        if ( ! empty($admin)) {
            return redirect('/app/admin');
        }
        $onetime_password = get_config('onetime_password') ?? 0;

        return view('account/login', ['onetime_password' => $onetime_password]);
    }

    /**
     * 仪表盘
     * @return void
     */
    public function dashboard()
    {
        $adminId   = get_admin_id();
        $adminInfo = admin();
        $roleIdArr = Auth::getGroups($adminId);
        $roleNames = [];
        foreach ($roleIdArr as $r) {
            $roleNames[] = $r['name'];
        }
        $adminInfo['group_name'] = implode(',', $roleNames);

        return view('index/dashboard', ['admin_info' => $adminInfo, 'sys_info' => $this->get_sys_info()]);
    }

    /**
     * phpinfo信息 按需显示在前台
     * @return array
     */
    private function get_sys_info()
    {
        $sys_upload_max_filesize = (int)convert_size_to_kb(ini_get('upload_max_filesize'));
        $php_upload_max_filesize = (int)(get_config('upload_maxsize'));
        $max_file_size           = max($sys_upload_max_filesize, $php_upload_max_filesize) * 1000;

        $sys_info['os']              = PHP_OS; //操作系统
        $sys_info['web_server']      = ''; //服务器环境
        $sys_info['phpv']            = phpversion(); //php版本
        $sys_info['fileupload']      = @ini_get('file_uploads') ? formatSize($max_file_size) : 'unknown'; //文件上传限制
        $sys_info['memory_limit']    = ini_get('memory_limit'); //最大占用内存
        $sys_info['domain']          = request()->host(); //域名
        $sys_info['remaining_space'] = round((@disk_free_space(".") / (1024 * 1024)), 2).'M'; //剩余空间
        $sys_info['beijing_time']    = gmdate("Y年n月j日 H:i:s", time() + 8 * 3600); //北京时间
        $sys_info['time']            = date("Y年n月j日 H:i:s"); //服务器时间
        $mysqlinfo                   = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version']   = $mysqlinfo[0]['version'];
        if (function_exists("gd_info")) {
            //GD库版本
            $gd                 = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }

        return $sys_info;
    }

}
